package com.fitns.user.service.baseConfig;

import java.util.Date;
import java.util.List;

import com.fitns.user.dao.baseConfig.TbFacilityTypeDao;
import com.fitns.user.dto.baseConfig.TbFacilityTypeDTO;
import com.fitns.user.dto.baseConfig.TbFacilityTypeVO;
import com.fitns.user.model.baseConfig.TbFacilityType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageInfo;

import javax.xml.validation.Validator;

@Service
public class TbFacilityTypeService {

	@Autowired
	private TbFacilityTypeDao tbFacilityTypeDao;
	
	/** 新增数据类型 */
	/*@Transactional
	public Integer insertFacilityType(TbFacilityTypeDTO tbFacilityTypeDTO) {
		TbFacilityType record  = new TbFacilityType();
		BeanUtils.copyProperties(tbFacilityTypeDTO, record);
		record.setId(tbFacilityTypeDTO.getId());
		record.setInputTime(new Date());
		record.setModifyTime(new Date());
		return tbFacilityTypeDao.insertSelective(record);
	}*/

	@Transactional
	public Integer insertFacilityType(TbFacilityTypeDTO tbFacilityTypeDTO,String facilityType) {
		TbFacilityType record  = new TbFacilityType();
		BeanUtils.copyProperties(tbFacilityTypeDTO, record);
		record.setId(tbFacilityTypeDTO.getId());
		record.setInputTime(new Date());
		TbFacilityType tbFacilityType = tbFacilityTypeDao.selectFacilityType(facilityType);
		if(tbFacilityType != null) {
			return 3; // 这里代表已经存在，不能添加。
		} 
		Integer result = tbFacilityTypeDao.insertSelective(record);
		if (result == 1) {
			return 1; // 这里代表添加成功。
		}else {
			return 2; // 这里代表添加失败。
		}
	}

	public List<TbFacilityTypeVO> selectAllType(TbFacilityTypeDTO tbFacilityTypeDTO) {
		List<TbFacilityTypeVO>  selectList =tbFacilityTypeDao.selectType(tbFacilityTypeDTO);
		return selectList;
	}

	@Transactional
	public int updateFacilityType(TbFacilityTypeDTO tbFacilityTypeDTO) {
		TbFacilityType record  = new TbFacilityType();
		BeanUtils.copyProperties(tbFacilityTypeDTO, record);
		record.setId(tbFacilityTypeDTO.getId());
		record.setModifyTime(new Date());
		return tbFacilityTypeDao.updateByPrimaryKeySelective(record);
	}
	
	public int deleteFacilityTypeById(long id) {
		
		return tbFacilityTypeDao.deleteFacilityTypeById(id);
		
	}
	
	/** 查看人员台账 */
	public PageInfo<TbFacilityTypeVO> selectBannerList(TbFacilityTypeDTO tbFacilityTypeDTO) {
		tbFacilityTypeDTO.pageHandle();
		List<TbFacilityTypeVO>  select =tbFacilityTypeDao.selectType(tbFacilityTypeDTO);
		PageInfo<TbFacilityTypeVO> sele = new PageInfo<>(select);
		return sele;
	}

	public TbFacilityType selectTbFacilityById(long id) {
		return tbFacilityTypeDao.selectTbFacilityById(id);
	}

	@Transactional
	public Integer inserIcon(TbFacilityTypeDTO tbFacilityTypeDTO) {
		TbFacilityType record  = new TbFacilityType();
		BeanUtils.copyProperties(tbFacilityTypeDTO, record);
		if(com.xiaoleilu.hutool.lang.Validator.isNull(tbFacilityTypeDTO.getId())){
			//record.setIcon(tbFacilityTypeDTO.getIcon());
			return this.insertFacilityType(tbFacilityTypeDTO, null);
		}else {
			record.setIcon(tbFacilityTypeDTO.getIcon());
			record.setModifyTime(new Date());
			return tbFacilityTypeDao.updateByPrimaryKey(record);
		}

	}

}
